<template>
	<div>
		<jk-dialog
			v-model="showDialog"
			title="物料明细"
			width="1160px"
			:show-content-loading="showLoading"
			:confirm-btn-loading="saveBtnLoading"
			:show-confirm-btn="pageType !== 'detail'"
			:show-cancel-btn="pageType !== 'detail'"
			:show-close-btn="pageType === 'detail'"
			append-to-body
			@on-close="onVisibleChange(false)"
			@on-cancel="onVisibleChange(false)"
			@on-visible-change="onVisibleChange"
			@on-confirm="onConfirmBtn"
		>
			<div class="flex-end-center-wrap">
				<jk-select v-model="searchParams.auditState" :option-list="auditStateList" placeholder="状态" clearable size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
				<jk-select v-model="searchParams.workshopId" :option-list="workshopList" placeholder="区域" size="small" class="search-bar-item-size2 margin-5-0 item-gap"></jk-select>
				<jk-search-input v-model="searchParams.nameOrCode" placeholder="编码或名称" size="small" class="search-bar-item-size3 margin-5-0 item-gap" clearable @on-enter="onSearchBtn" />
				<jk-search-button @click="onSearchBtn"></jk-search-button>
			</div>
			<jk-table
				ref="jkTable"
				:height="500"
				show-overflow
				border
				:data="tableData"
				:checkbox-config="{checkMethod: checkboxMethod}"
				@checkbox-change="onCheckBoxChange"
			>

				<vxe-column type="checkbox" align="center" :width="40"></vxe-column>
				<vxe-column title="编码" align="left" field="code" :min-width="120" />
				<vxe-column title="名称" align="left" field="name" :min-width="150" />
				<vxe-column title="规格型号" align="left" field="models" :min-width="150" />
				<vxe-column title="基准计量单位" align="left" field="unitName" :width="100" />
				<vxe-column title="存货类型" align="left" field="typeName" :width="100" />
				<vxe-column title="物料分类" align="left" field="categoryName" :width="150" />
				<vxe-column title="状态" align="center" field="auditState" :width="100">
					<template slot-scope="scope">
						<jk-common-status :status="scope.row.auditState"></jk-common-status>
					</template>
				</vxe-column>
			</jk-table>

			<!-- 分页组件 -->
			<footer ref="box2" class="flex-end-center margin-top-10">
				<jk-page
					:page-index="searchParams.pageNo"
					:page-sizes="searchParams.pageSizes"
					:page-size="searchParams.pageSize"
					:total="totalCount"
					@on-change="onPageChange"
				/>
			</footer>
		</jk-dialog>
	</div>
</template>

<script>
    import calHeight from '/src/mixins/cal-table-height';
    import { createTree, pageConfig } from '/src/utils/jk/jk-tool';
    import {
        spinProductPage,
        spinProductExportExcel
    } from '/src/api/material/material';
    import { auditStateList } from '../../../utils/jk/status-option-list';
    import { spinProductCategoryList } from '../../../api/material/category';
    import handlerCache from '../../../mixins/handler-cache';
    export default {
        mixins: [calHeight, handlerCache],
        components: { },
        props: {
            dataItem: {
                type: Object,
                default() {
                    return {};
                }
            },
            pageType: {
                type: String,
                default: ''
            },
            dialogState: {
                type: Boolean,
                default: false
            },
            defaultSelectRows: {
                type: Array,
                default() {
                    return [];
                }
            },
            request: {
                type: Function,
                require: true,
                default: null
            }
        },
        data() {
            return {
                apiItem: {
                    'page': spinProductPage,
                    'exportExcel': spinProductExportExcel
                },
                importDialogState: false,
                loading: false,
                selectTreeItem: {},
                treeData: [],
                detailDialogState: false,
                showLoading: true,
                saveBtnLoading: false,
                prefix: 'spin:product',
                auditStateList: auditStateList,
                dataId: null,
                showDialog: false,
                workshopList: [],
                saveDialogState: false,
                tableLoading: true,
                exportLoading: false,
                totalCount: 0,
                tableData: [],
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    nameOrCode: '',
                    auditState: null,
                    workshopId: null,
                    categoryId: null
                },
                checkArr: []
            };
        },
        methods: {
            // 勾选弹窗数据
            onCheckBoxChange({ records }) {
                this.checkArr = [];
                records.forEach(x => {
                    this.checkArr.push(x);
                    return x;
                });
            },
            checkboxMethod(e) {
                return !e.row._disabled;
            },
            setCheckState(arr) {
                arr.forEach((x) => {
                    x._disabled = false;
                    this.defaultSelectRows.forEach(k => {
                        if (x.code === k.productCode) {
                            x._disabled = true;
                        }
                    });
                });
                this.tableData = arr;
                this.tableData.forEach((x, index) => {
                    if (x._disabled) {
                        this.$refs.jkTable.$refs.xTable.setCheckboxRow([this.tableData[index], this.tableData[index]], true);
                    }
                });
            },
            // 弹窗确定
            onConfirmBtn() {
                this.saveBtnLoading = true;
                this.$emit('itemList', this.checkArr);
                this.$emit('on-visible-change', false);
                this.saveBtnLoading = false;
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            /** 查询列表 */
            getListRequest() {
                this.showLoading = true;
                return this.apiItem['page'](this.searchParams).then(res => {
                    this.showLoading = false;
                    if (res.data.status === 200) {
                        this.setCheckState(res.data.res || []);
                        this.tableData = res.data.res;
                        this.totalCount = res.data.count;
                    }
                });
            },
            async getDependentData() {
                this.showLoading = true;
                this.workshopList = this.$store.state.user.userWorkshop.workshopList;
                if (!this.searchParams.workshopId) this.$set(this.searchParams, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
                await this.getListRequest();
                this.showLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
